--星尘蔷薇龙
local s,id=GetID()
function s.initial_effect(c)
	Fusion.AddProcMix(c,true,true,44508094,73580471)
	Fusion.AddContactProc(c,s.contactfil,s.contactop,s.splimit,s.spcon)

	--summon
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_SINGLE)
	e3:SetCode(EFFECT_CANNOT_DISABLE_SPSUMMON)
	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	c:RegisterEffect(e3)

	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_REMOVE+CATEGORY_DAMAGE)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCountLimit(1)
	e1:SetTarget(s.distg)
	e1:SetOperation(s.disop)
	c:RegisterEffect(e1)
 
	--indes
	local e5=Effect.CreateEffect(c)
	e5:SetType(EFFECT_TYPE_SINGLE)
	e5:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e5:SetRange(LOCATION_MZONE)
	e5:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
	e5:SetValue(1)
	e5:SetCondition(s.stcon)
	c:RegisterEffect(e5)
	local e6=e5:Clone()
	e6:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
	c:RegisterEffect(e6)
	
	--伤害为0
	local e8=Effect.CreateEffect(c)
	e8:SetType(EFFECT_TYPE_FIELD)
	e8:SetCode(EFFECT_CHANGE_DAMAGE)
	e8:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e8:SetRange(LOCATION_MZONE)
	e8:SetCondition(s.stcon1)
	e8:SetTargetRange(1,0)
	e8:SetValue(0)
	c:RegisterEffect(e8)
	local e7=e8:Clone()
	e7:SetCode(EFFECT_NO_EFFECT_DAMAGE)
	c:RegisterEffect(e7)
	local e10=e8:Clone()
	e10:SetCode(EFFECT_NO_BATTLE_DAMAGE)
	c:RegisterEffect(e10)

	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetCategory(CATEGORY_NEGATE+CATEGORY_DESTROY)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_CHAINING)
	e2:SetCountLimit(1)
	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCondition(s.sdiscon)
	e2:SetCost(s.cost)
	e2:SetTarget(s.sdistg)
	e2:SetOperation(s.sdisop)
	c:RegisterEffect(e2)
	--disable spsummon
	local e31=Effect.CreateEffect(c)
	e31:SetDescription(aux.Stringid(id,1))
	e31:SetCategory(CATEGORY_DISABLE_SUMMON+CATEGORY_DESTROY)
	e31:SetType(EFFECT_TYPE_QUICK_O)
	e31:SetRange(LOCATION_MZONE)
	e31:SetCode(EVENT_SUMMON)
	e31:SetCondition(s.dscon)
	e31:SetCost(s.cost)
	e31:SetTarget(s.dstg)
	e31:SetOperation(s.dsop)
	c:RegisterEffect(e31)
	local e41=e31:Clone()
	e41:SetCode(EVENT_FLIP_SUMMON)
	c:RegisterEffect(e41)
	local e51=e31:Clone()
	e51:SetCode(EVENT_SPSUMMON)
	c:RegisterEffect(e51)
	--register summon
	aux.GlobalCheck(s,function()
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_SPSUMMON_SUCCESS)
		ge1:SetOperation(s.regop)
		Duel.RegisterEffect(ge1,0)
	end)

end
s.listed_names={44508094,73580471}

function s.reg(c)
	if c:IsCode(44508094) then
		Duel.RegisterFlagEffect(c:GetControler(),id,0,0,0)
	elseif c:IsCode(73580471) then
		Duel.RegisterFlagEffect(c:GetControler(),id+1,0,0,0)
	end
end
function s.regop(e,tp,eg,ep,ev,re,r,rp)
	eg:ForEach(s.reg)
end

function s.splimit(e,se,sp,st)
	return e:GetHandler():GetLocation()~=LOCATION_EXTRA 
end
function s.contactfil(tp)
	return Duel.GetMatchingGroup(Card.IsAbleToRemoveAsCost,tp,LOCATION_ONFIELD+LOCATION_GRAVE,0,nil)
end
function s.contactop(g)
	Duel.Remove(g,POS_FACEUP,REASON_COST+REASON_MATERIAL)
	Debug.ShowHint("超越次元的羁绊，梦之星尘与青蓝色火焰所孕育的圣洁之花啊！用你皎洁的羽毛扫灭世间一切的虚妄！绽放吧！星尘蔷薇龙！")
	Debug.Message("超越次元的羁绊，梦之星尘与青蓝色火焰所孕育的圣洁之花啊！用你皎洁的羽毛扫灭世间一切的虚妄！绽放吧！星尘蔷薇龙！") 
end
function s.spcon(tp)
	return Duel.GetFlagEffect(tp,id)~=0 and Duel.GetFlagEffect(tp,id+1)~=0
end

function s.rmfilter(c)
	return c:IsAbleToRemove()
end
function s.distg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.rmfilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,e:GetHandler()) end
	local g=Duel.GetMatchingGroup(Card.IsAbleToRemove,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,e:GetHandler())
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,g:GetCount(),0,0)
	Duel.SetChainLimit(s.chainlm)
end
function s.chainlm(e,rp,tp)
	return tp==rp
end
function s.disop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.rmfilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,e:GetHandler())
	if g:GetCount()>0 then
	Duel.Remove(g,POS_FACEUP,REASON_EFFECT)
	Duel.Damage(1-tp,g:GetCount()*600,REASON_EFFECT)
	end
end

function s.rpfilter(c)
	return c:IsCode(44508094) 
end
function s.rpfilter1(c)
	return c:IsCode(73580471) 
end
function s.stcon(e)
	return  Duel.IsExistingMatchingCard(s.rpfilter,e:GetHandlerPlayer(),LOCATION_REMOVED,0,1,nil)
end
function s.stcon1(e)
	return Duel.IsExistingMatchingCard(s.rpfilter1,e:GetHandlerPlayer(),LOCATION_REMOVED,0,1,nil)
end


function s.sdiscon(e,tp,eg,ep,ev,re,r,rp)
	return rp~=tp and not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) and Duel.IsChainNegatable(ev)
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return c:IsAbleToRemoveAsCost() end
	if Duel.Remove(c,POS_FACEUP,REASON_COST+REASON_TEMPORARY)~=0 then
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		e1:SetCode(EVENT_PHASE+PHASE_END)
		e1:SetReset(RESET_PHASE+PHASE_END)
		e1:SetLabelObject(c)
		e1:SetCountLimit(1)
		e1:SetOperation(s.retop)
		Duel.RegisterEffect(e1,tp)
	end
end
function s.retop(e,tp,eg,ep,ev,re,r,rp)
	Duel.ReturnToField(e:GetLabelObject())
end
function s.sdistg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_NEGATE,eg,1,0,0)
	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
		Duel.SetOperationInfo(0,CATEGORY_DESTROY,eg,1,0,0)
	end
end
function s.sdisop(e,tp,eg,ep,ev,re,r,rp)
	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
		Duel.Remove(eg,POS_FACEUP,REASON_EFFECT)
	end
end
function s.dscon(e,tp,eg,ep,ev,re,r,rp)
	return tp~=ep and Duel.GetCurrentChain()==0
end
function s.dstg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_DISABLE_SUMMON,eg,#eg,0,0)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,eg,#eg,0,0)
end
function s.dsop(e,tp,eg,ep,ev,re,r,rp)
	Duel.NegateSummon(eg)
	Duel.Remove(eg,POS_FACEUP,REASON_EFFECT)
end